package com.ktgj.customer.controller;

import com.alibaba.fastjson.JSON;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.customer.domain.Bank;
import com.ktgj.customer.domain.BankCard;
import com.ktgj.customer.domain.ContactInformation;
import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.vo.BankCardVo;
import com.ktgj.customer.domain.vo.BankInformation;
import com.ktgj.customer.domain.vo.BankVo;
import com.ktgj.customer.service.IBankCardService;
import com.ktgj.customer.service.IBankService;
import com.ktgj.customer.service.IContactlnformationService;
import com.ktgj.customer.service.ILoginService;
import com.ktgj.customer.util.CheckBankCard;
import com.ktgj.customer.util.Clients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 联系方式Controller
 */
@ControllerAdvice
@RestController
@RequestMapping("/Contaclnformation")
public class ContaclnformationController extends BaseController
{
    @Autowired
    private IContactlnformationService contactlnformationService;
    @Autowired
    private ILoginService loginService;

    /**
     * 获取当前用户的ID
     * @param request
     * @return
     */

    public Long customerId(HttpServletRequest request){
        Long customerId = loginService.getLoginCustomer(request).getCustomerId();
        return customerId;
    }

    /**
     * 后端——查询联系方式
     * */
    @PostMapping("/backConnect")
    public AjaxResult backContactInformationById(@RequestBody Map<String, Object> map) {
        if (map.get("customerId") != null && !map.get("customerId").equals("")){
            Long customerId = Long.valueOf(map.get("customerId").toString());

            List<ContactInformation> contactInformations;
            try {
                contactInformations = contactlnformationService.backContactInformationById(customerId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (contactInformations != null && contactInformations.size() > 0){
                return AjaxResult.success(contactInformations);
            }
            else {
                return AjaxResult.error("查询失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 后端——查询待修改联系方式
     * */
    @PostMapping("/backUpdateConnect")
    public AjaxResult backUpdateContactInformation(@RequestBody Map<String, Object> map) {
        if (map.get("contactInformationId") != null && !map.get("contactInformationId").equals("")){
            ContactInformation contactInformation;
            try {
                Long contactInformationId = Long.valueOf(map.get("contactInformationId").toString());
                contactInformation = contactlnformationService.backUpdateContactInformation(contactInformationId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (contactInformation != null){
                return AjaxResult.success(contactInformation);
            }
            else {
                return AjaxResult.error("查询失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 后端——修改联系方式
     * */
    @PostMapping("/backUpdateConnectById")
    public AjaxResult backUpdateContactInformationById(@RequestBody ContactInformation contactInformation) {
        if (contactInformation != null){
            int i;
            try {
                i = contactlnformationService.backUpdateContactInformationById(contactInformation);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (i > 0){
                return AjaxResult.success("修改成功");
            }
            else {
                return AjaxResult.error("修改失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 后端——新增联系方式
     * */
    @PostMapping("/backInsertConnect")
    public AjaxResult backInsertContactInformationById(HttpServletRequest request, @RequestBody ContactInformation contactInformation){
        Long customerId;
        if (contactInformation.getCustomerId() != null && contactInformation.getCustomerId() != 0){
            customerId = contactInformation.getCustomerId();
        }
        else {
            try {
                customerId = customerId(request);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
        }

        if (contactInformation != null){
            int i;
            try {
                contactInformation.setCustomerId(customerId);
                i = contactlnformationService.backInsertContactInformationById(contactInformation);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (i > 0){
                return AjaxResult.success("新增成功");
            }
            else {
                return AjaxResult.error("新增失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 后端——删除联系方式
     * */
    @PostMapping("/backDeleteConnect")
    public AjaxResult backDeleteContactInformationById(@RequestBody Map<String, Object> map){
        if (map.get("contactInformationId") != null && !map.get("contactInformationId").equals("")){
            int i;
            try {
                Long contactInformationId = Long.valueOf(map.get("contactInformationId").toString());
                i = contactlnformationService.backDeleteContactInformationById(contactInformationId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (i > 0){
                return AjaxResult.success("删除成功");
            }
            else {
                return AjaxResult.error("删除失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }
}
